//
//  SettingViewController.swift
//  ERecycle-Shop
//
//  Created by abb on 16/9/8.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit
let SettingCellReuseIdentifier = "SettingViewCellReuseIdentifier"
class SettingViewController: AbbBaseViewController {

    var settingCellArr:[[String:String]] = [
        ["image":"location","title":"地址管理"],
        ["image":"setting","title":"设置"],
        ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 添加tableView
        view.addSubview(tableView)
        // 设置代理数据源
        tableView.delegate = self
        tableView.dataSource = self
        
    }

    
    // MARK: - 懒加载
    /// 用户操作tableView
    private lazy var tableView:UITableView = UITableView(frame: self.view.frame, style: .Grouped)
    /// 退出登录
    private lazy var alertController:UIAlertController = {
        let alertController = UIAlertController(title: "你确定要退出吗?", message: "退出", preferredStyle: UIAlertControllerStyle.ActionSheet)
        let actionDone = UIAlertAction(title: "退出登录", style: .Destructive, handler: { (action) in
            User.exitUser()
            let userDefaults = NSUserDefaults.standardUserDefaults()
            userDefaults.removeObjectForKey("lastLoginUser")
            NSNotificationCenter.defaultCenter().postNotificationName(ABBRootViewControllerKey, object: false)
        })
        let actionCancel = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
        alertController.addAction(actionDone)
        alertController.addAction(actionCancel)
        return alertController
    }()
    
    
}


extension SettingViewController:UITableViewDelegate,UITableViewDataSource {
    // MARK: - UITableViewDataSource
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section.isIn(0) {
            return 2
        }
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.section.isIn(0) {
            let settingCell = UITableViewCell(style: .Default, reuseIdentifier: SettingCellReuseIdentifier)
            settingCell.imageView!.image = UIImage(named: settingCellArr[indexPath.row]["image"]!)
            settingCell.textLabel!.text = settingCellArr[indexPath.row]["title"]
            return settingCell
        }
        
        let exitCell = tableView.dequeueStaticCell(indexPath)
        exitCell.textLabel?.text = "退出登录"
        exitCell.textLabel?.textAlignment = NSTextAlignment.Center
        exitCell.textLabel?.textColor = UIColor.redColor()
        return exitCell
    }
    
    // MARK: - UITableViewDelegate
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 48
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.section == 1 {
            self.presentViewController(alertController, animated: true, completion: nil)
        }
        
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
    }
}
